/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entities;

import bookmaker.*;
import entities.*;
import bookmaker.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.faces.context.FacesContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author ALIEN
 */
public class GameList {
    private List list;
    private String confirmation;
    private double choice;
    private Session session;

    /**
     * @return the list
     */
    public List getList() {
        Map map = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        Object o = map.get("g");
        if(o==null) return null;
        long id = Long.parseLong(o+"");
        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("select distinct g from Game g left join fetch g.league as leag where leag.id ="+id);
            list = (ArrayList) q.list();
            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return list;
    }

    public String getConfirmation(){
        Map map = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        Object o = map.get("id");
        if(o==null) return null;
        long id = Long.parseLong(o+"");
        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("select distinct m from Market m where m.id ="+id);
            list = (ArrayList) q.list();
            tx.commit();
            Market m = ((Market)(list.get(0)));
            //confirmation = new String("Rynek "+m.getName()+": "+
            //        m.getOdds_away()+" "+m.getOdds_draw()+" "+m.getOdds_home());
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return confirmation;
    }

    public double getChoice(){
        Map map = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        Object o = map.get("id");
        if(o==null) return -1;
        long id = Long.parseLong(o+"");
        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("select distinct m from Market m where m.id ="+id);
            list = (ArrayList) q.list();
            tx.commit();
            Market m = ((Market)(list.get(0)));
            o = map.get("m");
            choice=-1;
            if(o.toString().equals("h"))
                choice=m.getOdds_home();
            else if(o.toString().equals("a"))
                choice=m.getOdds_away();
            else if(o.toString().equals("d"))
                choice=m.getOdds_draw();

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return choice;
    }
}
